//
//  VoiceMsgCell.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/22.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "VoiceMsgCell.h"

@interface VoiceMsgCell ()

@property (nonatomic, strong) MASConstraint *leftPlayLayout;
@property (nonatomic, strong) MASConstraint *rightPlayLayout;

@property (nonatomic, strong) MASConstraint *leftDurationLayout;
@property (nonatomic, strong) MASConstraint *rightDurationLayout;

@end

@implementation VoiceMsgCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    self.playImgView = [[UIImageView alloc] initWithImage:UIImage.k_imgName(@"")];
    
    self.durationLabel = [UILabel k_labelWithFontSize:13 textColor:UIColor.k_t2Color];
    self.durationLabel.numberOfLines = 0;
    self.durationLabel.preferredMaxLayoutWidth = Chat_MsgContent_MaxWidth - Chat_MsgContent_Padding*2;
    
    [self.msgContentView addSubview:self.playImgView];
    [self.msgContentView addSubview:self.durationLabel];
    

    [self.playImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(Chat_MsgContent_Padding);
        make.bottom.mas_offset(-Chat_MsgContent_Padding);
        self.leftPlayLayout = make.left.mas_offset(Chat_MsgContent_Padding);
        self.rightPlayLayout = make.right.mas_offset(-Chat_MsgContent_Padding);
        make.size.mas_equalTo(CGSizeMake(12, 18));
    }];
    [self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.playImgView);
        self.leftDurationLayout = make.left.equalTo(self.playImgView.mas_right).offset(5);
        self.rightDurationLayout = make.right.equalTo(self.playImgView.mas_left).offset(-5);
    }];
    [self.msgContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(Chat_MsgContent_MaxWidth / 3);
    }];
    
    
}

- (void)setMsgDirection:(int)msgDirection {
    [super setMsgDirection:msgDirection];
    
    if (msgDirection == 1) {
        self.msgContentView.backgroundColor = UIColor.k_t1Color;
        self.playImgView.image = UIImage.k_imgName(@"chat_voice_send2");
        [self.leftPlayLayout deactivate];
        [self.rightPlayLayout activate];
        
        [self.leftDurationLayout deactivate];
        [self.rightDurationLayout activate];
        
        UIImage *img1 = UIImage.k_imgName(@"chat_voice_send0");
        UIImage *img2 = UIImage.k_imgName(@"chat_voice_send1");
        UIImage *img3 = UIImage.k_imgName(@"chat_voice_send2");
        self.playImgView.animationImages = @[img1, img2, img3];
        self.playImgView.animationDuration = 1.5;
        
    } else {
        self.msgContentView.backgroundColor = UIColor.k_hexColor(0xe0e0e0);
        self.playImgView.image = UIImage.k_imgName(@"chat_voice_receive2");
        UIImage *img1 = UIImage.k_imgName(@"chat_voice_receive0");
        UIImage *img2 = UIImage.k_imgName(@"chat_voice_receive1");
        UIImage *img3 = UIImage.k_imgName(@"chat_voice_receive2");
        self.playImgView.animationImages = @[img1, img2, img3];
        self.playImgView.animationDuration = 1.5;
        [self.leftPlayLayout activate];
        [self.rightPlayLayout deactivate];
        
        [self.leftDurationLayout activate];
        [self.rightDurationLayout deactivate];
    }
}

- (void)setMsgModel:(MsgModel *)msgModel {
    [super setMsgModel:msgModel];
    if (msgModel.direction == MsgDirection_send) {
        self.durationLabel.textColor = UIColor.whiteColor;
    } else {
        self.durationLabel.textColor = UIColor.k_t2Color;
    }
    self.voiceMsg = msgModel.msg;
    
    if (msgModel.playing == YES) {
        [self.playImgView startAnimating];
    } else {
        [self.playImgView stopAnimating];
    }
}

- (void)setVoiceMsg:(MsgItem *)voiceMsg {
    _voiceMsg = voiceMsg;
    

    self.durationLabel.text = [NSString stringWithFormat:@"%ld″", voiceMsg.second];
    CGFloat msgW = voiceMsg.second/60.0 * (Chat_MsgContent_MaxWidth*0.8);
    if (msgW < 60) {
        msgW = 60;
    }
    [self.msgContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(msgW);
    }];
}

@end
